package com.bls.cate.ui.activity.register;

import android.content.Intent;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.EmptyUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bls.blslib.base.BaseActivity;
import com.bls.cate.R;
import com.bls.cate.constant.Interface;
import com.bls.cate.listener.EditTextChangedListener;
import com.bls.cate.model.RootRes;
import com.bls.cate.utils.VCodeUtil;
import com.google.gson.Gson;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.Objects;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/1/6.
 */

public class RegisterActivity extends BaseActivity {
    public static final int SET_RESULT_CODE = 100;
    private EditText etPhone, etVcode, etPwd;
    private ImageView btnBack, btnCancel, btnVcode;
    private TextView btnRegister, btnLogin;
    private String strPhone;
    private String strPwd;
    private String vCode;   //验证码

    @Override
    protected void initParams() {
        super.initParams();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        TextView tvTopTitle = findViewById(R.id.title_middle_text);
        tvTopTitle.setText("注册");
        btnBack = findViewById(R.id.title_left_iv_2);
        btnBack.setVisibility(View.VISIBLE);

        etPhone = findViewById(R.id.et_phone);
        etVcode = findViewById(R.id.et_vcode);
        etPwd = findViewById(R.id.et_pwd);

        btnCancel = findViewById(R.id.btn_iv_cancel);
        btnVcode = findViewById(R.id.btn_vcode);
        btnRegister = findViewById(R.id.btn_register);
        btnLogin = findViewById(R.id.btn_login);

        btnVcode.setImageBitmap(VCodeUtil.getInstance().createBitmap());
        vCode = VCodeUtil.getInstance().getCode().toLowerCase();

        btnRegister.setBackground(getDrawable(R.drawable.bg_btn_login_register_2));

        String strBtnRegister = "<font>已有账号 </font><u><font color='#66CCFF'>去登陆</font></u>";
        btnLogin.setText(Html.fromHtml(strBtnRegister));
    }


    @Override
    protected void initListener() {
        EditTextChangedListener editTextChangedListener = new EditTextChangedListener(getActivity(), etPhone, etPwd, etVcode, btnRegister);
        setOnClickListener(btnBack, btnCancel, btnVcode, btnRegister, btnLogin);
        etPhone.addTextChangedListener(editTextChangedListener);
        etVcode.addTextChangedListener(editTextChangedListener);
        etPwd.addTextChangedListener(editTextChangedListener);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_iv_2:
                finish();
                break;
            case R.id.btn_iv_cancel:
                etPhone.setText(null);
                break;
            case R.id.btn_vcode:
                btnVcode.setImageBitmap(VCodeUtil.getInstance().createBitmap());
                vCode = VCodeUtil.getInstance().getCode().toLowerCase();
                break;
            case R.id.btn_register:
                btnEventRegister();
                break;
            case R.id.btn_login:
                finish();
                break;
        }
    }

    /**
     * 注册事件
     */
    private void btnEventRegister() {
        strPhone = etPhone.getText().toString().trim();
        String strVcode = etVcode.getText().toString().trim();
        strPwd = etPwd.getText().toString().trim();
        if (EmptyUtils.isEmpty(strPhone)) ToastUtils.showShort("请输入手机号");
        else if (EmptyUtils.isEmpty(strVcode)) ToastUtils.showShort("请输入验证码");
        else if (EmptyUtils.isEmpty(strPwd)) ToastUtils.showShort("请输入密码");
        else if (!RegexUtils.isMobileSimple(strPhone)) ToastUtils.showShort("手机号格式错误");
        else if (!Objects.equals(strVcode, vCode)) ToastUtils.showShort("验证码错误");
        else if (strPwd.length() < 6) ToastUtils.showShort("密码长度至少6位");
        else {
            presenter.requestStrData(1, Interface.INTERFACE_ACCOUNT, CacheMode.NO_CACHE,
                    new HttpParams("RequestMethod", "Register"),//接口类型
                    new HttpParams("Phone", strPhone),//手机号
                    new HttpParams("Password", strPwd));//密码
        }
    }

    @Override
    public void showOnStart(Request request, int requestId) {
        loadingDogDialog.show();
    }

    @SuppressWarnings("unchecked")
    @Override
    public void showOnSuccess(int requestId, String responseBody, Response response) {
        if (requestId == 1) {
            RootRes rootRes = new Gson().fromJson(responseBody, RootRes.class);
            LogUtils.i(rootRes.isResultStatus());
            if (rootRes.isResultStatus()) {
                ToastUtils.showShort("注册成功");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent();
                        intent.putExtra("phone", strPhone);
                        intent.putExtra("password", strPwd);
                        setResult(SET_RESULT_CODE, intent);
                        finish();
                    }
                }, 1500);
            } else
                ToastUtils.showShort(rootRes.getResultDes());
        }
    }

    @Override
    public void showOnFinish(int requestId) {
        loadingDogDialog.dismiss();
    }
}
